package datastructure.search;

/**
 * 插值查找算法
 * @author lbw
 */
public class InsertValueSearch {

    public static void main(String[] args) {
        int[] arr = new int[100];
        for (int i = 0;i < 100; i++){
            arr[i] = i +1;
        }
        System.out.println(insertValueSearch(arr, 0, arr.length-1, 18));
    }

    public static int insertValueSearch(int[] arr, int left, int right, int target){
        if (arr[left] > target || arr[right] < target || left > right){
            return -1;
        }

        int mid = left + (right - left) * (target - arr[left]) / (arr[right] - arr[left]);
        int midVal = arr[mid];
        if (target > midVal){
            return insertValueSearch(arr, mid + 1, right, target);
        } else if (target < midVal){
            return insertValueSearch(arr, left, mid -1, target);
        } else{
            return mid;
        }
    }
}
